//
//  SSNavigationController.swift
//  SSSina
//
//  Created by ss on 2017/6/29.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit

class SSNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //重写设置手势代理
        interactivePopGestureRecognizer?.delegate = self
    }

    //返回
    @objc private func backClick(){
        
        popViewController(animated: true)
    }
    //监听用户使用边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //如果为true ,意思为接收手势,反之为不接收
        return (childViewControllers.count != 1)
    }
    
    
    //重写父类以后的方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //判断count 大于0 ,(一级界面去掉)
        if childViewControllers.count > 0 {
            //设置默认值
            var title = "返回"
            
            //二级界面 显示时上一个界面的title 其他的是显示返回
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.navigationItem.title ?? ""
                //隐藏tabBar
                viewController.hidesBottomBarWhenPushed = true
            }
            
            //设置左侧按钮
            //如果自定义了左侧导航按钮 会导致点击屏幕左侧时 侧滑手势失效
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
        }
        
        //监听该方法完成左侧按钮的设置
        super.pushViewController(viewController, animated: animated)
    }
}
